<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <meta name="description" content="Your description goes here" />
        <meta name="keywords" content="power control electricity meter" />
        <meta name="author" content="Benny Saxen" />
        <link rel="stylesheet" type="text/css" href="home.css" title="home" media="screen,projection" />
<?
session_start(); 
$g_id        = $_SESSION['g_id'];
$g_width     = $_SESSION['g_width'];
$g_height    = $_SESSION['g_height'];
$g_scale     = $_SESSION['g_scale'];
$g_ymax      = $_SESSION['g_ymax'];
$g_filter    = $_SESSION['g_filter'];

$s_year[1]   = $_SESSION['s_year1'];
$s_month[1]  = $_SESSION['s_month1'];
$s_day[1]    = $_SESSION['s_day1'];
$s_hour[1]   = $_SESSION['s_hour1'];
$s_min10[1]  = $_SESSION['s_min10_1'];
$s_scope[1]  = $_SESSION['s_scope1'];

$n_day    = date('d');
$n_month  = date('m');
$n_year   = date('Y');
$n_sec    = date('s');
$n_min    = date('i');
$n_hour   = date('H');

// GET
//=======================================
$action  = $_GET['action'];
//=======================================
if($action == 'id')
{
   $g_id  = $_GET['id'];
}

//=======================================
$action  = $_POST['f_event'];
//=======================================
if($action == 'home_config')
{
   $temp  = $_POST['window'];
   editHomeConfig($g_id,"WINDOW",$temp);
   $temp  = $_POST['zoom'];
   editHomeConfig($g_id,"ZOOM",$temp);
   $temp  = $_POST['step'];
   editHomeConfig($g_id,"STEP",$temp);
   $temp  = $_POST['tolerance'];
   editHomeConfig($g_id,"TOLERANCE",$temp);
   $temp  = $_POST['threshold'];
   editHomeConfig($g_id,"THRESHOLD",$temp);
}


$_SESSION['g_id']      = $g_id;
$_SESSION['g_width']   = $g_width;
$_SESSION['g_height']  = $g_height;
$_SESSION['g_scale']   = $g_scale;
$_SESSION['g_ymax']    = $g_ymax;
$_SESSION['g_filter']  = $g_filter;

$_SESSION['s_year1']   = $s_year[1];
$_SESSION['s_month1']  = $s_month[1];
$_SESSION['s_day1']    = $s_day[1];
$_SESSION['s_hour1']   = $s_hour[1];
$_SESSION['s_min10_1'] = $s_min10[1];
$_SESSION['s_scope1']  = $s_scope[1];

readHomeConfig($g_id);
echo("</head><body>");
echo("$n_year $n_month $n_day<br>");
echo("<a href=\"home.php?id=$g_id\">$g_id</a><br>");

echo("<table border=0>");
echo("<form action=\"bessConfig.php\" method='POST' enctype='multipart/form-data'>");
echo("<input name=\"f_event\"  type=\"hidden\" value=\"home_config\"/>");
echo("<tr><th>Window</th><th><input name=\"window\"  type=\"text\" size=\"5\" value=\"$g_windowX\"/></th></tr>");
echo("<tr><th>Zoom</th><th><input name=\"zoom\"  type=\"text\" size=\"5\" value=\"$g_zoom\"/></th></tr>");
echo("<tr><th>Step</th><th><input name=\"step\"  type=\"text\" size=\"5\" value=\"$g_step\"/></th></tr>");
echo("<tr><th>Tolerance</th><th><input name=\"tolerance\"  type=\"text\" size=\"5\" value=\"$g_tolerance\"/></th></tr>");
echo("<tr><th>Threshold</th><th><input name=\"threshold\"  type=\"text\" size=\"5\" value=\"$g_threshold\"/></th></tr>");
echo("<tr><th><input type=\"submit\" value=\"Uppdatera\" name=\"submit_file\"></th></tr></form>");
echo("</table>");

echo("</body></html>");
//==========================================
function showFileContent($file)
//==========================================
{
  $handle = fopen($file, "r");
  if ($handle) {
    while (($line = fgets($handle)) !== false) {
        echo("<p>$line</p>");
    }
  } else {
    echo("error");
  } 
  fclose($handle);
}
//==========================================
function readHomeConfig($id)
//==========================================
{
  global $g_windowX,$g_windowY,$g_zoom,$g_step,$g_tolerance,$g_threshold;;
  $file = "home-config-".$id.".bess"; 
  $handle = fopen($file, "r");
  if ($handle) 
  {
    while (($line = fgets($handle)) !== false) 
    {
		if(strstr($line,"WINDOW:")) sscanf($line,"%s %d",$temp,$g_windowX);
		if(strstr($line,"ZOOM:")) sscanf($line,"%s %d",$temp,$g_zoom);
	    if(strstr($line,"STEP:")) sscanf($line,"%s %d",$temp,$g_step);
	    if(strstr($line,"TOLERANCE:")) sscanf($line,"%s %d",$temp,$g_tolerance);
		if(strstr($line,"THRESHOLD:")) sscanf($line,"%s %d",$temp,$g_threshold);
    }
    fclose($handle);
  } 
}
//==========================================
function editHomeConfig($id,$key,$val)
//==========================================
{
  $file1 = "home-temp-".$id.".bess"; 
  $out = fopen($file1, 'w');
  if(!$out)homeError($id,"Unable to open tempfile editHomeConfig $id,$key,$val");
        	
  $file2 = "home-config-".$id.".bess"; 
  $handle = fopen($file2, "r");
  if ($handle) 
  {
    while (($line = fgets($handle)) !== false) 
    { 
		if(strstr($line,$key))
		{
			$content = $key.": ".$val."\n";fwrite($out,$content);
		}
		else
		    fwrite($out,$line);
    }
    fclose($handle);
    fclose($out);
  } 
  system("cp -f $file1 $file2");
}
?>
